using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Threading;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
using Fycayy.Common;
using common;

namespace GoldenAgeHelper.common
{
    public class GAClient
    {
        public static HttpClient client = new HttpClient();
        static GAClient()
        {
            client.Encoding = Encoding.UTF8;
            Log.clearTime = 10000;
        }

        private static Log log = new Log("log/request.log");

        public static string ClientUrl(string url, NameValueCollection data)
        {
            string text = "";
            lock (client)
            {
                byte[] b = client.UploadValues(url, data);
                text = Encoding.UTF8.GetString(b);

                string datastr = "";
                foreach (string key in data.Keys)
                {
                    datastr += key + ":" + data[key] + ",";
                }
                if (datastr.Length > 0)
                {
                    datastr = datastr.Substring(0, datastr.Length - 1);
                }
                log.Append("POST:" + url + " params:" + datastr);

            }
            return text;
        }

        public static string ClientUrl(string url)
        {
            return ClientUrl(url, new NameValueCollection());
        }

        public static string ClientUrl(string url, string name, string value)
        {
            NameValueCollection data = new NameValueCollection();
            data.Add(name, value);
            return ClientUrl(url, data);
        }

        public static string DownloadString(string url)
        {
            lock (client)
            {
                try
                {
                    log.Append("GET:" + url);
                    return client.DownloadString(new Uri(url));
                }
                catch
                {
                    return client.DownloadString(new Uri(url));
                }
            }
        }

        public static string PostString(string url, NameValueCollection data)
        {
            return ClientUrl(url, data);
        }

        public static void SaveConfig(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(fs, GAClient.client.Cookies);
            fs.Close();
        }

        public static bool LoadConfig(string path)
        {
            if (!File.Exists(path))
            {
                return false;
            }
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            if (fs.Length > 0)
            {
                CookieContainer con = bf.Deserialize(fs) as CookieContainer;
                if (con != null)
                {
                    client.Cookies = con;
                    fs.Close();
                    return true;
                }
            }
            fs.Close();
            return false;
        }
    }
}
